// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var list;
var app = angular.module('blog', ['ionic', 'blog.service'])

.controller('index',function($scope, $jd, $location){
  function search(){
    var querystring=window.location.search.substring(1);
    var gets={};
    var vars=querystring.split('&')
    var param;
    for(var i=0;i<vars.length;i++){
        param=vars[i].split('=');
        gets[param[0]]=param[1];
    }
    return gets;
  }
  $scope.loading = true;

  var path = location.href;
  var current = '';
  if(path.indexOf('?') == -1){
    current = 'index';
  }else{
    var param = search();
    if(param!= undefined && param.id != undefined){
      current = 'detail';
    }else{
      current = 'index';
    }
  }
  list = {};
  $scope.currentpage = $location.search().p || 1;
  $scope.count = 0;
  //列表数据监听
  $scope.request={
      url:"index",
      params:$location.search()
  };

  // $scope.show = function() {
  //   $ionicLoading.show({
  //     template: '<i class="icon ion-refreshing"></i>',
  //     duration: 4000
  //   });
  // };
  // $scope.hide = function(){
  //   $ionicLoading.hide();
  // };

  $jd.watch($scope).success(function(data){
    $scope.loading = false;
    var data = data.data;
    $scope.totalpage = data.totalPages;
    $scope.lists = data.list;
    $scope.count = data.total;
    if(param!= undefined && param.id != undefined)
      $scope.detail = $scope.lists[param.id];
  });

  //点击分页时触发
  $scope.gotopage=function(page){
    $scope.loading = true;
    $scope.currentpage = page;
    $scope.request.params.p=page;
    $(window).scrollTop(0);
  };
  $scope.current = current;
})

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
});
